package indi.zhifa.study2024.class002.busy.generalTest.business.report.consign.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.zhifa.study2024.class002.busy.generalTest.business.report.consign.entity.mapperIn.ConsignMapperIn;
import indi.zhifa.study2024.class002.busy.generalTest.business.report.consign.entity.mapperOut.ConsignMapperOut;
import indi.zhifa.study2024.class002.busy.generalTest.business.report.consign.entity.request.*;
import indi.zhifa.study2024.class002.busy.generalTest.business.report.consign.entity.response.*;
import indi.zhifa.study2024.class002.busy.generalTest.business.report.consign.mapper.ConsignMapper;
import indi.zhifa.study2024.class002.busy.generalTest.business.report.consign.service.IConsignReportService;
import indi.zhifa.study2024.core.common.web.entity.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
public class ConsignReportServiceImpl implements IConsignReportService {

    private final ConsignMapper mConsignMapper;

    protected <IN extends BaseConsignReportRequest,OUT extends BaseConsignReportResponse> Page<OUT>  generalPageConsignReport(IN pConsignGroupByItemReportRequest,
                                                                       Class<? extends BaseConsignReportResponse> pOutClass){
        ConsignMapperIn consignMapperIn = pConsignGroupByItemReportRequest.toMapperIn();
        List<ConsignMapperOut> consignMapperOut = mConsignMapper.consignReport(consignMapperIn);
        Page<OUT> rtn = new Page<>(pConsignGroupByItemReportRequest.getCurrent(),pConsignGroupByItemReportRequest.getSize());
        List<OUT> responseList = new ArrayList<>();
        for(ConsignMapperOut consignMapperOutItem : consignMapperOut){
            try {
                OUT consignGroupByItemReportResponse = (OUT)pOutClass.getConstructor().newInstance();
                consignGroupByItemReportResponse.initWithMapperOut(consignMapperOutItem);
                consignGroupByItemReportResponse.cal();
                responseList.add(consignGroupByItemReportResponse);
            }catch (Exception ex){
                throw new ServiceException("反射失败"+ex.getMessage());
            }

        }
        rtn.setRecords(responseList);
        return rtn;
    }

    @Override
    public Page<ConsignGroupByItemReportResponse> pageConsignGroupByItem(ConsignGroupByItemReportRequest pConsignGroupByItemReportRequest) {

        Page<ConsignGroupByItemReportResponse> rtn = generalPageConsignReport(pConsignGroupByItemReportRequest,ConsignGroupByItemReportResponse.class);
        return rtn;
    }

    @Override
    public Page<ConsignGroupByBrandReportResponse> pageConsignGroupByBrand(ConsignGroupByBrandReportRequest pConsignGroupByBrandReportRequest) {
        Page<ConsignGroupByBrandReportResponse> rtn = generalPageConsignReport(pConsignGroupByBrandReportRequest,ConsignGroupByBrandReportResponse.class);
        return rtn;
    }

    @Override
    public Page<ConsignGroupBySalesReportResponse> pageConsignGroupBySales(ConsignGroupBySalesReportRequest pConsignGroupBySalesReportRequest) {
        Page<ConsignGroupBySalesReportResponse> rtn = generalPageConsignReport(pConsignGroupBySalesReportRequest,ConsignGroupBySalesReportResponse.class);
        return rtn;
    }

    @Override
    public Page<ConsignGroupByCustomerReportResponse> pageConsignGroupByCustomer(ConsignGroupByCustomerReportRequest pConsignGroupByCustomerReportRequest) {
        Page<ConsignGroupByCustomerReportResponse> rtn = generalPageConsignReport(pConsignGroupByCustomerReportRequest,ConsignGroupByCustomerReportResponse.class);
        return rtn;
    }

    @Override
    public Page<ConsignGroupByCustomerAndItemReportResponse> pageConsignGroupByCustomerAndItem(ConsignGroupByCustomerAndItemReportRequest pConsignGroupByCustomerAndItemReportRequest) {
        Page<ConsignGroupByCustomerAndItemReportResponse> rtn = generalPageConsignReport(pConsignGroupByCustomerAndItemReportRequest,ConsignGroupByCustomerAndItemReportResponse.class);
        return rtn;
    }
}
